/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.living.player.ui.recently.album;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.living.R;
import com.living.filedownloader.ui.TasksDownloadingActivity;
import com.living.player.db.RecentlyManger;
import com.living.player.ui.recently.audio.RecentlyAudioBatchFragment;
import com.living.widget.BottomMenuView;
import com.md.album.db.SelfAlbumManager;
import com.md.basedpc.constant.BaseConstant;
import com.md.basedpc.routerservice.NotifyService;
import com.md.datastore.MysaveAlbumManager;
import com.md.livingwidget.CoreBaseActivity;
import com.md.livingwidget.toast.PHToast;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import java.util.Map;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * 自建、收藏、最近播放专辑删除页面
 */
@Route(path = "/selfcreateorsave/album")
public class RecentlyAlbumBatchActivity extends CoreBaseActivity {
    @Autowired(name = BaseConstant.ALBUM_TYPE)
    public int currentType = -1; // 1:最近播放， 2：自建专辑 3：收藏的专辑

    BottomMenuView bottom_menu;
    RecentlyAlbumBatchFragment audioBatchFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recently_album_batch_delete);
        ARouter.getInstance().inject(this);
        FragmentManager fragmentManager = getSupportFragmentManager();
//        audioBatchFragment = (RecentlyAlbumBatchFragment) fragmentManager.findFragmentById(R.id.fm_batch_delete);
        audioBatchFragment = RecentlyAlbumBatchFragment.newInstance(currentType);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.flt_batch_delete, audioBatchFragment);
        transaction.commit();

        bottom_menu = findViewById(R.id.bottom_menu);
        bottom_menu.setMenuShows(false, false, false, true);
        bottom_menu.setMenuDeleteEnable(false);

        audioBatchFragment.setOnBatchSelectLisenter(new RecentlyAlbumBatchFragment.OnBatchSelectLisenter() {
            @Override
            public void onSelect(int count, int total) {
                if (count > 0) {
                    bottom_menu.setMenuDeleteEnable(true);
                } else {
                    bottom_menu.setMenuDeleteEnable(false);
                }
            }
        });
        bottom_menu.setOnBottomMenuViewClick(new BottomMenuView.OnBottomMenuViewClick() {
            @Override
            public void onClickAdd(View view) {
                PHToast.showSingleToast(getmActivity(), "去李越专辑添加页面");
            }

            @Override
            public void onClickDownload(View view) {

            }

            @Override
            public void onClickShare(View view) {
                PHToast.showSingleToast(getmActivity(), "分享组件");
            }

            @Override
            public void onClickDelete(View view) {
                Map<String, Album> mapSel = audioBatchFragment.getMapSel();
                if (mapSel.size() > 0) {
                    setResult(Activity.RESULT_OK);
                    //遍历map中的值
                    for (Album value : mapSel.values()) {
                        if (currentType == BaseConstant.SELF_CREATE_ALBUM) {
                            if (SelfAlbumManager.getIns().deleteAlbumById(value.getId())) {
                                ARouter.getInstance().navigation(NotifyService.class).selfAlbumUpdateSuc(0);
                            }
                        } else if (currentType == BaseConstant.SAVE_ALBUM) {
                            MysaveAlbumManager.getIns().deleteAlbumById(value.getId());
                        } else {
                            RecentlyManger.getInstance().deleteAlbumById(value.getId());
                        }
                        mapSel.remove(value);
                        audioBatchFragment.remove(value);
                    }
                } else {
                    PHToast.showSingleToast(getmActivity(), "请选择要删除的");
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.layout_downloading) {
            startActivity(new Intent(this, TasksDownloadingActivity.class));
        } else if (v.getId() == R.id.iv_downloaded_edite) {

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

//    private void initData() {
//        Intent intent = getIntent();
//        currentType = intent.getIntExtra(BaseConstant.ALBUM_TYPE, -1);
//    }

}
